package hong.jun.pet_home.controller.org;

import hong.jun.basic.util.AjaxResult;
import hong.jun.basic.util.PageList;
import hong.jun.pet_home.domain.util.EmailMessage;
import hong.jun.pet_home.domain.util.EmailMessageType;
import hong.jun.pet_home.domain.org.Shop;
import hong.jun.pet_home.domain.util.SendEmailUtil;
import hong.jun.pet_home.query.ShopQuery;
import hong.jun.pet_home.service.org.IShopService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * restfull风格
 *
 * @Controller
 * @ResponseBody
 */

@RestController
@RequestMapping("/shop")
//@CrossOrigin//后台的跨域解决方案-spring4.2.5及以上版本支持
public class ShopController {
    private static final Logger logger = LoggerFactory.getLogger(ShopController.class);
    @Autowired
    private IShopService shopService;
    @Autowired //用来发送邮件的工具类
    private SendEmailUtil sendEmailUtil;

    /**
     * 新增或修改
     */
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Shop shop) {
        //判断有无id决定新增还是修改
        try {
            if (shop.getId() != null) {
                shopService.update(shop);
            } else {
                shopService.save(shop);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("失败" + e.toString());
        }
    }

    /**
     * 单个删除
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        try {
            Shop shop = shopService.getById(id);
            logger.info("删除店铺" + shop.toString());//未查到对象为空会报空指针异常
            shopService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            logger.error(e.toString());
            return new AjaxResult(e.toString());
        }
    }

    /**
     * 根据id 批量删除
     */ //localhost:80/deparment/14,13
    @PostMapping("/dels")
    public AjaxResult dels(@RequestBody Long[] ids) {
        try {
            shopService.deletes(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            logger.error(e.toString());
            return new AjaxResult(e.toString());
        }
    }

    /**
     * 根据Id查询单个
     */
    @GetMapping(value = "/{id}")
    public Shop getById(@PathVariable("id") Long id) {
        return shopService.getById(id);
    }

    /**
     * 条件分页查询
     */
    @PostMapping
    public PageList<Shop> getPage(@RequestBody ShopQuery query) {
        return shopService.queryPage(query);
    }

    /**
     * 入驻通过   给用户发邮件，用户点击激活
     */
    @PostMapping("/audityes")
    public AjaxResult audityes(@RequestBody Shop shop) {
        try {
            EmailMessage emailMessage = EmailMessageType.SHOP_SETTLED_PASS.shopEvent(shop);
            return sendEmailUtil.sendEmail(emailMessage);
        } catch (Exception e) {
            logger.error(e.toString());
            return new AjaxResult("激活邮件发送失败");
        }
    }

    /**
     * 入驻驳回
     */
    @DeleteMapping("/audityno")
    public AjaxResult auditno(@RequestBody Shop shop) {
        try {
            EmailMessage emailMessage = EmailMessageType.SHOP_SETTLED_REJECT.shopEvent(shop);
            return sendEmailUtil.sendEmail(emailMessage);
        } catch (Exception e) {
            logger.error(e.toString());
            return new AjaxResult("审核驳回邮件发送失败");
        }
    }

    /**
     * 店铺禁用
     */
    @PutMapping("/forbidden")
    public AjaxResult auditForbidden(@RequestBody Shop shop) {
        AjaxResult ajaxResult = shopService.audit(shop);
        if (ajaxResult.getMessage() == "操作成功!") {
            try {//持久化成功才发邮件
                EmailMessage emailMessage = EmailMessageType.SHOP_FORBIDDEN.shopEvent(shop);
                return sendEmailUtil.sendEmail(emailMessage);
            } finally {//邮件无论发送成功都返会成功 用户已被禁用
                return AjaxResult.me();
            }
        } else {
            return ajaxResult;
        }
    }
}
